package com.thprojects.myphotomemories.view.components;

import java.util.List;

import android.content.Context;
import android.graphics.Bitmap;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Gallery;
import android.widget.ImageView;

import com.thprojects.myphotomemories.R;
import com.thprojects.myphotomemories.controller.FSController;
import com.thprojects.myphotomemories.model.Photo;

import exception.PhotoSourceNotFoundException;
import exception.SDNotReadyException;

public class PhotoAdapter extends BaseAdapter{
	private Context mContext;
	private List<Photo> mPhotos;
	int mHeight;
	FSController mFsController;
	
	public PhotoAdapter(Context context, List<Photo> photos, int height){
		mContext = context;
		mPhotos = photos;
		mHeight = height;
		mFsController = FSController.getFSController(context);
	}
	
	public int getCount() {
		return mPhotos.size();
	}

	public Object getItem(int position) {
		return mPhotos.get(position);
	}

	public long getItemId(int position) {
		return mPhotos.get(position).getId();
	}

	public View getView(int position, View convertView, ViewGroup parent) {
		LayoutInflater inflater = LayoutInflater.from(mContext);
		View v = inflater.inflate(R.layout.photo_adapter, parent, false);
		
		Gallery.LayoutParams glp = new Gallery.LayoutParams(mHeight-12, mHeight-12);
		v.setLayoutParams(glp);
		
		ImageView mImage = (ImageView) v.findViewById(R.id.photo_img_preview);
		
		Photo p = mPhotos.get(position);

		Bitmap img;
		try {
			img = mFsController.decodeSampledBitmap(p.getPath(), mHeight, mHeight);
			mImage.setImageBitmap(img);
		} catch (PhotoSourceNotFoundException e) {
		} catch (SDNotReadyException e) {
		}
    	
	
		
		return v;
	}
	
}
